package org.wwb.antbox.user.service.menu;

import org.ant.commons.commons.query.enums.Sort;
import org.ant.commons.models.model.BaseIdModel;
import org.ant.commons.models.page.PageInfo;
import org.ant.commons.models.result.BaseEditResult;
import org.ant.commons.models.result.BasePageResult;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.wwb.antbox.user.data.menu.SysMenuMapper;
import org.wwb.antbox.user.iservice.menu.ISysMenuService;
import org.wwb.antbox.user.model.menu.convert.SysMenuToTree;
import org.wwb.antbox.user.model.menu.entity.SysMenu;
import org.wwb.antbox.user.model.menu.enums.SysMenuTypeEnum;
import org.wwb.antbox.user.model.menu.model.*;
import org.wwb.antbox.user.model.menu.view.SysMenuPageView;
import org.wwb.antbox.user.model.menu.view.SysMenuTreeView;
import org.wwb.antbox.user.model.menu.view.SysMenuTreeViewNew;
import org.wwb.antbox.user.model.system.entity.SysSystem;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Bond
 * @Date: 2019/10/4 20:00
 * @Description: 系统接口实现
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysMenuServiceImpl extends BaseService<SysMenuMapper, SysMenu, String> implements ISysMenuService {

    /**
     * 获取菜单类型为模块的菜单集合
     * @param systemCode 系统编号
     * @return List<SysMenu>
     */
    private List<SysMenu> getMenuNotSuper(String systemCode){
        Query query = new Query()
                .eq("menu_type", SysMenuTypeEnum.MODEL.getCode())
                .eq("system_code",systemCode)
                .orderby("menu_sort", Sort.ASC);
        // 查询出所有菜单
        List<SysMenu> sysMenuList = mapper.list(query);
        return sysMenuList;
    }

    /**
     * 获取功能菜单表格树数据
     * @param sysMenuTreeGridModel 分页查询参数模型
     * @return BasePageResult<SysMenuPageView>
     */
    @Override
    public BasePageResult<SysMenuPageView> getSysMenuTreeGrid(SysMenuTreeGridModel sysMenuTreeGridModel) {
        Query build = Query.build(sysMenuTreeGridModel)
                .orderby("menu_sort", Sort.ASC);

        // 查询出所有菜单
        List<SysMenu> allSysMenu = mapper.list(build);

        // 查询出菜单类型为模块的菜单集合
        List<SysMenu> returnList = new ArrayList<>();
        returnList.addAll(this.getMenuNotSuper(sysMenuTreeGridModel.getSystemCode()));

        // 获取每个顶层元素的子数据集合
        for(SysMenu sysMenu : returnList){
            sysMenu.setChildren(getSysMenuTreeGridChild(sysMenu.getId(), allSysMenu));
        }

        PageInfo<SysMenu> sysMenuPageInfo = new PageInfo<>();
        sysMenuPageInfo.setList(returnList);

        PageInfo<SysMenuPageView> sysMenuPageViewPageInfo = new SysMenuPageView().toPageView(sysMenuPageInfo);

        BasePageResult<SysMenuPageView> basePageResult = new BasePageResult<>();
        basePageResult.setModel(sysMenuPageViewPageInfo);
        basePageResult.setSuccess(true);
        return basePageResult;
    }

    /**
     * 递归查找功能菜单表格树数据子节点
     * @param id 当前菜单id
     * @param allSysMenu 要查找的列表
     * @return
     */
    private List<SysMenu> getSysMenuTreeGridChild(String id, List<SysMenu> allSysMenu){
        List<SysMenu> childList=new ArrayList<>();
        String parentId;

        //子集的直接子对象
        for (SysMenu sysMenu : allSysMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            parentId = sysMenu.getpMenuId();
            if(StringUtils.isNoneBlank(parentId)){
                if(parentId.equals(id)){
                    childList.add(sysMenu);
                }
            }
        }

        //子集的间接子对象
        for(SysMenu sysMenu : childList){
            sysMenu.setChildren(getSysMenuTreeGridChild(sysMenu.getId(), allSysMenu));
        }

        //递归退出条件
        if(childList.size()==0){
            return null;
        }
        return childList;
    }


    /**
     * 获取功能菜单树数据
     * @param sysMenuTreeModel 系统菜单树Model
     * @return BaseResult<List<SysMenuTreeView>>
     */
    @Override
    public BaseResult<List<SysMenuTreeView>> getSysMenuTree(SysMenuTreeModel sysMenuTreeModel) {
        Query build = Query.build(sysMenuTreeModel)
                .notEq("menu_type",SysMenuTypeEnum.BUTTON.getCode())
                .orderby("menu_sort", Sort.ASC);

        // 查询出所有菜单
        List<SysMenu> allSysMenu = mapper.list(build);

        // 把所有菜单转换成Tree对象
        List<SysMenuTreeView> allTreeList = SysMenuToTree.convertSysMenuToTree(allSysMenu);

        // 查询出菜单类型为模块的菜单集合
        List<SysMenu> returnList = new ArrayList<>();
        returnList.addAll(this.getMenuNotSuper(sysMenuTreeModel.getSystemCode()));

        // 把菜单类型为模块的菜单集合转换成Tree对象
        List<SysMenuTreeView> returnTreeList = SysMenuToTree.convertSysMenuToTree(returnList);

        //获取每个顶层元素的子数据集合
        for(SysMenuTreeView tree : returnTreeList){
            tree.setChildren(getSysMenuTreeChildList(tree.getValue(), allTreeList));
        }

        BaseResult<List<SysMenuTreeView>> baseResult = new BaseResult<>();
        baseResult.setModel(returnTreeList);
        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     * 递归查找功能菜单树数据子节点
     * @param id 当前菜单id
     * @param allTreeList 要查找的列表
     * @return
     */
    private List<SysMenuTreeView> getSysMenuTreeChildList(String id, List<SysMenuTreeView> allTreeList){
        List<SysMenuTreeView> childList=new ArrayList<>();
        String parentId;

        //子集的直接子对象
        for (SysMenuTreeView tree : allTreeList) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            parentId = tree.getpId();
            if(StringUtils.isNoneBlank(parentId)){
                if(parentId.equals(id)){
                    childList.add(tree);
                }
            }
        }

        //子集的间接子对象
        for(SysMenuTreeView tree : childList){
            tree.setChildren(getSysMenuTreeChildList(tree.getValue(), allTreeList));
        }

        //递归退出条件
        if(childList.size()==0){
            return null;
        }
        return childList;
    }
    /**
     * 获取菜单树4添加角色
     * @param sysSystemList 系统集合
     * Map<String,List<SysMenuTreeView>>
     * key: 系统编号
     * value：菜单树集合
     * @return BaseResult<Map<String,List<SysMenuTreeViewNew>>>
     */
    @Override
    public BaseResult<Map<String, List<SysMenuTreeViewNew>>> getSysMenuTree4AddRole(List<SysSystem> sysSystemList) {

        if(sysSystemList.isEmpty()){
            return null;
        }

        Query query = new Query().orderby("menu_sort", Sort.ASC);

        // 查询出所有菜单
        List<SysMenu> allSysMenu = mapper.list(query);

        // 把所有菜单转换成Tree对象
        List<SysMenuTreeViewNew> allTreeList = SysMenuToTree.convertSysMenuToTree4AddRoleNew(allSysMenu);

        /**
         * Map<key,value>
         * key: 系统编号
         * value：菜单树集合
         */
        Map<String, List<SysMenuTreeViewNew>> returnModel = new LinkedHashMap<>();

        for(SysSystem sysSystem : sysSystemList){

            // 查询出菜单类型为模块的菜单集合
            List<SysMenu> returnList = this.getMenuNotSuper(sysSystem.getSystemCode());

            // 把菜单类型为模块的菜单集合转换成Tree对象
            List<SysMenuTreeViewNew> returnTreeList = SysMenuToTree.convertSysMenuToTree4AddRoleNew(returnList);

            //获取每个顶层元素的子数据集合
            for(SysMenuTreeViewNew tree : returnTreeList){
                tree.setChildren(getSysMenuTreeChildListNew(tree.getValue(), allTreeList));
            }
            returnModel.put(sysSystem.getSystemName(),returnTreeList);
        }

        BaseResult<Map<String, List<SysMenuTreeViewNew>>> baseResult = new BaseResult<>();
        baseResult.setModel(returnModel);
        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     * 递归查找功能菜单树数据子节点
     * @param id 当前菜单id
     * @param allTreeList 要查找的列表
     * @return
     */
    private List<SysMenuTreeViewNew> getSysMenuTreeChildListNew(String id, List<SysMenuTreeViewNew> allTreeList){
        List<SysMenuTreeViewNew> childList=new ArrayList<>();
        String parentId;

        //子集的直接子对象
        for (SysMenuTreeViewNew tree : allTreeList) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            parentId = tree.getpId();
            if(StringUtils.isNoneBlank(parentId)){
                if(parentId.equals(id)){
                    childList.add(tree);
                }
            }
        }

        //子集的间接子对象
        for(SysMenuTreeViewNew tree : childList){
            tree.setChildren(getSysMenuTreeChildListNew(tree.getValue(), allTreeList));
        }

        //递归退出条件
        if(childList.size()==0){
            return null;
        }
        return childList;
    }

    @Override
    public BaseResult<Integer> saveSysMenu(SysMenuAddModel sysMenuAddModel) {
        SysMenu sysMenu = sysMenuAddModel.modelToEntity();
        int save = mapper.save(sysMenu);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(save);
        baseResult.setSuccess(save == 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> delSysMenu(SysMenuDelModel sysUserDelModel) {
        Query query = new Query().in("id",sysUserDelModel.getIds());

        int delete = mapper.deleteByQuery(query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(delete);
        baseResult.setSuccess(delete >= 1);
        return baseResult;
    }

    @Override
    public BaseEditResult doEditSysMenu(BaseIdModel<String> baseIdModel) {
        SysMenu sysUser = mapper.getById(baseIdModel.getId());
        BaseEditResult baseEditResult = new BaseEditResult();
        baseEditResult.setModel(sysUser);
        baseEditResult.setSuccess(true);
        return baseEditResult;
    }

    @Override
    public BaseResult<Integer> editSysMenu(SysMenuEditModel sysUserEditModel) {
        SysMenu sysUser = sysUserEditModel.modelToEntity();
        int edit = mapper.updateIgnoreNull(sysUser);
        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(edit);
        baseResult.setSuccess(edit >= 1);
        return baseResult;
    }
}
